from PyQt6.QtWidgets import  QWidget,QVBoxLayout,QHBoxLayout,QPushButton,QTableWidget,QHeaderView,QTableWidgetItem,QMenu
from PyQt6.QtCore import QSize,Qt
from db.DbmsDao import DbmsDao
from utils.Logger import LogPrint
from dialog import ModifyDbmsDialog

class DbmsMgrView(QWidget):
    def __init__(self, parent=None,date=None):
        super(DbmsMgrView, self).__init__(parent)
        # 初始化变量

        self.init_ui()

    def init_ui(self):

        layout_all=QVBoxLayout()

        layout_btn=QHBoxLayout()

        btn_add=QPushButton('添加')
        btn_add.setFixedSize(QSize(50,30))
        btn_add.clicked.connect(self.on_clicked_btn_add)
        layout_btn.addWidget(btn_add)

        btn_delete=QPushButton('删除')
        btn_delete.setFixedSize(QSize(50,30))
        btn_delete.clicked.connect(self.on_clicked_btn_delete)
        layout_btn.addWidget(btn_delete)

        layout_all.addLayout(layout_btn)

        self.create_table_dbms_list()

        layout_all.addWidget(self.table_dbms)

        self.reflash_table()

        self.setLayout(layout_all)

    #创建数据库列表布局
    def create_table_dbms_list(self):
        self.table_dbms = QTableWidget()
        self.table_dbms.setRowCount(0)
        self.table_dbms.setColumnCount(8) 

         # 为 QTableWidget 设置右键菜单
        self.table_dbms.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
        self.table_dbms.customContextMenuRequested.connect(self.on_right_clicked)

        #设置widget整体高度
        #self.table_bill_detail.setFixedHeight(400) 
        # 设置表格头部标题
        self.table_dbms.setHorizontalHeaderLabels(['ID', '名称', '类型', 'host','port', '用户名','密码','备注'])

        self.table_dbms.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
        self.table_dbms.verticalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)

   #刷新table数据
    def reflash_table(self):
        self.list_dbms=DbmsDao.get_db_info()
        if self.list_dbms != None and len(self.list_dbms) > 0:
            
            # 设置表格行数和列数
            row_count=len(self.list_dbms)
            self.table_dbms.setRowCount(row_count)  
        
            # 添加数据到表格
            for row in range(self.table_dbms.rowCount()):
                for col in range(self.table_dbms.columnCount()):
                    item = str(self.list_dbms[row][col])
                    self.table_dbms.setItem(row, col, QTableWidgetItem(item))
                #table.setRowHeight(row,30)

        LogPrint.debug('reflash_table finish')

    def on_clicked_btn_add(self):
        self.modify_dbms_dialog = ModifyDbmsDialog.Ui_Dialog()
        self.modify_dbms_dialog.show()

    def on_clicked_btn_delete(self):
        pass

            #，处理右键点击
    def on_right_clicked(self,point):
         # 创建一个 QMenu 对象
        menu = QMenu(self)
        
        # 添加菜单项
        set_item = menu.addAction("修改")
        # 连接动作触发信号到槽函数
        set_item.triggered.connect(self.on_menu_clicked_modify)
        
        # 执行菜单项被点击的操作
        action = menu.exec(self.table_dbms.mapToGlobal(point))

     #设置菜单事件
    def on_menu_clicked_modify(self):
        row=self.table_dbms.currentRow()
        print('on_menu_clicked_set_norelation row',row,len(self.list_dbms))
        if row < len(self.list_dbms):
            c_id=self.list_dbms[row][0]
            self.modify_dbms_dialog = ModifyDbmsDialog.Ui_Dialog(c_id=c_id,callback=self.reflash_table)
            self.modify_dbms_dialog.show()